<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title></title>
    <script src="../comment.js"></script>
    <script src="https://www.jq22.com/jquery/jquery-1.7.1.js"></script>
    <link rel="stylesheet" href="./layer/theme/default/layer.css">
    <link rel="stylesheet" type="text/css" href="./css/reponse.css">
    <style type="text/css">
        .alertceng {
            background: #FFFFFF;
            padding: 10px;
            display: none;
        }

        .alertceng span.title {
            margin-right: 10px;
        }
    </style>
</head>
<body>
<div class="layui-fluid layadmin-homepage-fluid" style="background: #fff;width:90%;margin:0 auto;">
    <div id="page">
        <div class="btn-container">
            <button class="layui-btn" onclick="addtr();">新增库存信息</button>
            <button class="layui-btn" onclick="ExportExcel();">导出为Excel</button>
            <!--<button class="layui-btn" onclick="resivetabledata();">获取table数据</button>-->
            <button class="layui-btn" onclick="reloadtable();">刷新库存信息</button>
            <!--<button class="layui-btn" onclick="coldata();">获取某一列的数据</button>-->
        </div>
        <table id="table" class="reponsetable"></table>
        <script type="text/html" id="editer">
            <a class="layui-btn layui-btn-xs" onclick="edittr(this)">编辑</a>
            <a class="layui-btn layui-btn-xs" onclick="deletetr(this)">删除</a>
            <a class="layui-btn layui-btn-xs" onclick="uptr(this)">上移</a>
            <a class="layui-btn layui-btn-xs" onclick="downtr(this)">下移</a>
        </script>
    </div>
</div>
<div id="editcontent" class="alertceng">   <!--编辑库存-->
    <p><span class="title">药品名称:</span><input type="text" id="Name"/></p>
    <p><span class="title">药品位置:</span><input type="text" id="Address"/></p>
    <p><span class="title">药品库存:</span><input type="text" id="Remain"/></p>
</div>

<div id="addcontent" class="alertceng">   <!--增加库存-->
    <p><span class="title">新增药品名称:</span><input type="text" id="aName"/></p>
    <p><span class="title">新增药品位置:</span><input type="text" id="aAddress"/></p>
    <p><span class="title">新增药品库存:</span><input type="text" id="aRemain"/></p>
</div>


</body>

<script type="text/javascript" src="./js/jquery.basictable.min.js"></script>
<script type="text/javascript" src="./reponsetable.min.js"></script>
<script type="text/javascript" src="layer/layer.js"></script>
<script type="text/javascript">

    $(function () {
        getdata()

    })

    function addtr() {
        layer.open({
            type: 1,
            title: '新增库存信息',
            closeBtn: 1,
            area: '100%',
            skin: '#fff', //没有背景色
            shadeClose: true,
            content: $('#addcontent'),
            btn: ["保存", "关闭"],
            btn1: function (index, layero) {
                var Name = $("#Name").val();
                var Remain = $("#Remain").val();
                var Address = $("#Address").val();
                var obj = {
                    "Name": Name,
                    "Address": Address,
                    "Remain": Remain
                };
                reponse.addtr(obj, "table");
                //reponse.editsavetr(obj, "table");
                layer.close(index);
            }, btn2: function (index, layero) {
                layer.close(index);
            }
        });
    };

    function uptr(btn, e) {
        var tr = $(btn).parent().parent();
        reponse.moveuptr(tr, "table");  //上移
    }

    function downtr(btn, e) {
        var tr = $(btn).parent().parent();
        reponse.moveDown(tr, "table");  //下移
    }

    function ExportExcel() {
        var tableobj = $("#table").data("tableObj");
        reponse.JSONToCSVConvertor(tableobj, true, "药品库存表格");
    }

    // function resivetabledata() {
    //     var pp = $("#table").data("tableObj").data;
    //     alert(JSON.stringify(pp));
    // }

    function reloadtable() {   //刷新
        var data = [
            getdata()
        ]
        reponse.reloadtable(data, "table");
    }

    function edittr(a, e) {
        var tr = $(a).parent().parent();
        reponse.resiverowdata(tr, "table");
        var row = $("#table").data("rowd");
        var rowdata = $("#table").data("rowdata");
        //alert(JSON.stringify(rowdata));
        //页面层  获取某一列数据

        layer.open({
            type: 1,
            title: '编辑库存信息',
            closeBtn: 1,
            area: '100%',
            skin: '#fff', //没有背景色
            shadeClose: true,
            content: $('#editcontent'),
            btn: ["保存", "关闭"],
            btn1: function (index, layero) {
                var Name = $("#Name").val();
                var Remain = $("#Remain").val();
                var Address = $("#Address").val();
                var obj = {
                    "Name": Name,
                    "Address": Address,
                    "Remain": Remain
                };
                // reponse.addtr(obj, "table");
                reponse.editsavetr(obj, "table");

                layer.close(index);
            }, btn2: function (index, layero) {
                layer.close(index);
            }
        });
    }

    function deletetr(a, e) {
        var tr = $(a).parent().parent();
        reponse.deletetr(tr, e);  //删除
    }

    // function coldata() {
    //     var pp = reponse.Columndata("Name", "table");
    //     alert(pp);  //获取某一列数据
    // }

    function  getdata() {
        $.ajax({
            type: "GET",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: MyServer+"h5/yao/0" ,//url
            headers: {
                "Authorization" : sessionStorage.getItem("Authorization"),
            },
            success: function (result) {
                if(result.code==200){
                    var datas=[];
                    for(var i=0;i<result.data.length;i++){
                        var t = {"Name": result.data[i].mName, "Address": result.data[i].mAddress, "Remain": result.data[i].mLiange};
                        datas.push(t);
                    }
                    $('#table').basictable({
                        breakpoint: 768
                    });
                    $("#table").reponsetable({
                        "id": "table",
                        "operation": "editer",
                        "type": "numbers",
                        "colum": [
                            {"field": "Name", "title": "药品名称"},
                            {"field": "Address", "title": "药品位置"},
                            {"field": "Remain", "title": "现有库存"},
                        ],
                        "data": datas
                    });

                }else if(result.code===401){
                            alert("您未登陆")
                        }else if(result.code===405){
                    alert(result.message);
                        }else {
                            alert("服务器开小差了！")
                        }
            },
            error:function(data){
                if(data.status==401){
                alert("您未登陆")
            }else if(data.code===405){
                    alert(data.message);
                }else{
                alert("服务器开小差了！")
            }
            }
        });
    }



</script>
</html>